import time

import numpy as np
import pandas as pd


input_example = ['cpu_us:vm:172.28.2.197:1617253563719:-0.802',
                 'cpu_id:vm:172.28.2.197:1617253563723:1.802',
                 'cpu_io:vm:172.28.2.197:1617253563725:0.014',
                 'cpu_ir:vm:172.28.2.197:1617253563729:0.0',
                 'cpu_ni:vm:172.28.2.197:1617253563732:0.000222',
                 'cpu_so:vm:172.28.2.197:1617253563734:0.003111',
                 'cpu_st:vm:172.28.2.197:1617253563737:0.0',
                 'cpu_sy:vm:172.28.2.197:1617253563740:0.048667',
                 'cpu_ur:vm:172.28.2.197:1617253563743:0.095333',
                 'mem_to:vm:172.28.2.197:1617253563746:16791924736.0',
                 'mem_us:vm:172.28.2.197:1617253563749:0.149448',
                 'mem_bu:vm:172.28.2.197:1617253563751:0.006684',
                 'mem_ca:vm:172.28.2.197:1617253563754:0.084108',
                 'mem_fr:vm:172.28.2.197:1617253563756:0.850552',
                 'mem_sr:vm:172.28.2.197:1617253563759:0.00439',
                 'mem_su:vm:172.28.2.197:1617253563762:0.004312',
                 'disk_r:vm:172.28.2.197:1617253563764:1326461440.0',
                 'disk_w:vm:172.28.2.197:1617253563767:3194421760.0',
                 'net_o:vm:172.28.2.197:1617253563769:33217.733333',
                 'net_i:vm:172.28.2.197:1617253563772:34087.533333',
                 'intr:vm:172.28.2.197:1617253563775:142957393.0',
                 'mem_to:vm:172.28.2.197:1617253563778:8909073.066667',
                 'cpu_us:vm:172.28.2.198:1617253563786:-0.652889',
                 'cpu_id:vm:172.28.2.198:1617253563789:1.652889',
                 'cpu_io:vm:172.28.2.198:1617253563794:0.226889',
                 'cpu_ir:vm:172.28.2.198:1617253563797:0.0',
                 'cpu_ni:vm:172.28.2.198:1617253563800:0.0',
                 'cpu_so:vm:172.28.2.198:1617253563803:0.001556',
                 'cpu_st:vm:172.28.2.198:1617253563805:0.0',
                 'cpu_sy:vm:172.28.2.198:1617253563808:0.039333',
                 'cpu_ur:vm:172.28.2.198:1617253563811:0.063333',
                 'mem_to:vm:172.28.2.198:1617253563814:16791941120.0',
                 'mem_us:vm:172.28.2.198:1617253563817:0.164377',
                 'mem_bu:vm:172.28.2.198:1617253563820:0.006738',
                 'mem_ca:vm:172.28.2.198:1617253563822:0.111301',
                 'mem_fr:vm:172.28.2.198:1617253563825:0.835623',
                 'mem_sr:vm:172.28.2.198:1617253563828:0.021213',
                 'mem_su:vm:172.28.2.198:1617253563830:0.00601',
                 'disk_r:vm:172.28.2.198:1617253563833:946872832.0',
                 'disk_w:vm:172.28.2.198:1617253563835:101161882112.0',
                 'net_o:vm:172.28.2.198:1617253563838:2595.533333',
                 'net_i:vm:172.28.2.198:1617253563841:3470.533333',
                 'intr:vm:172.28.2.198:1617253563843:73035560.25',
                 'mem_to:vm:172.28.2.198:1617253563846:61622.044444',
                 'cpu_us:vm:172.28.2.199:1617253563850:-0.869778',
                 'cpu_id:vm:172.28.2.199:1617253563853:1.869778',
                 'cpu_io:vm:172.28.2.199:1617253563856:0.062',
                 'cpu_ir:vm:172.28.2.199:1617253563859:0.0',
                 'cpu_ni:vm:172.28.2.199:1617253563861:0.000222',
                 'cpu_so:vm:172.28.2.199:1617253563864:0.000667',
                 'cpu_st:vm:172.28.2.199:1617253563867:0.0',
                 'cpu_sy:vm:172.28.2.199:1617253563870:0.028',
                 'cpu_ur:vm:172.28.2.199:1617253563873:0.033333',
                 'mem_to:vm:172.28.2.199:1617253563875:16791924736.0',
                 'mem_us:vm:172.28.2.199:1617253563878:0.169377',
                 'mem_bu:vm:172.28.2.199:1617253563881:0.006286',
                 'mem_ca:vm:172.28.2.199:1617253563883:0.116739',
                 'mem_fr:vm:172.28.2.199:1617253563886:0.830623',
                 'mem_sr:vm:172.28.2.199:1617253563889:0.02124',
                 'mem_su:vm:172.28.2.199:1617253563891:0.006006',
                 'disk_r:vm:172.28.2.199:1617253563894:943030784.0',
                 'disk_w:vm:172.28.2.199:1617253563896:6533145088.0',
                 'net_o:vm:172.28.2.199:1617253563904:2559.666667',
                 'net_i:vm:172.28.2.199:1617253563914:3511.533333',
                 'intr:vm:172.28.2.199:1617253563917:19613587.75',
                 'mem_to:vm:172.28.2.199:1617253563919:74274.133333',
                 'cpu_us:vm:172.28.2.200:1617253563925:-0.797556',
                 'cpu_id:vm:172.28.2.200:1617253563929:1.797556',
                 'cpu_io:vm:172.28.2.200:1617253563932:0.010444',
                 'cpu_ir:vm:172.28.2.200:1617253563945:0.0',
                 'cpu_ni:vm:172.28.2.200:1617253563948:0.000222',
                 'cpu_so:vm:172.28.2.200:1617253563951:0.004',
                 'cpu_st:vm:172.28.2.200:1617253563953:0.000222',
                 'cpu_sy:vm:172.28.2.200:1617253563956:0.055111',
                 'cpu_ur:vm:172.28.2.200:1617253563959:0.102222',
                 'mem_to:vm:172.28.2.200:1617253563962:16791924736.0',
                 'mem_us:vm:172.28.2.200:1617253563964:0.136026',
                 'mem_bu:vm:172.28.2.200:1617253563967:0.00661',
                 'mem_ca:vm:172.28.2.200:1617253563970:0.072071',
                 'mem_fr:vm:172.28.2.200:1617253563973:0.863974',
                 'mem_sr:vm:172.28.2.200:1617253563976:0.004134',
                 'mem_su:vm:172.28.2.200:1617253563978:0.004291',
                 'disk_r:vm:172.28.2.200:1617253563981:1205498368.0',
                 'disk_w:vm:172.28.2.200:1617253563984:3099554304.0',
                 'net_o:vm:172.28.2.200:1617253563986:32540.266667',
                 'net_i:vm:172.28.2.200:1617253563989:33419.266667',
                 'intr:vm:172.28.2.200:1617253563992:146767845.0',
                 'mem_to:vm:172.28.2.200:1617253563995:1296975.644444']
output_example = ['cpu_us:vm:172.28.2.197:1624781081589:-0.802', 'cpu_id:vm:172.28.2.197:1624781081589:1.802', 'cpu_io:vm:172.28.2.197:1624781081589:0.014', 'cpu_ir:vm:172.28.2.197:1624781081589:0.0', 'cpu_ni:vm:172.28.2.197:1624781081589:-1.0', 'cpu_so:vm:172.28.2.197:1624781081589:0.003111', 'cpu_st:vm:172.28.2.197:1624781081589:0.0', 'cpu_sy:vm:172.28.2.197:1624781081589:0.048667', 'cpu_ur:vm:172.28.2.197:1624781081589:0.095333', 'mem_to:vm:172.28.2.197:1624781081589:-1.0', 'mem_us:vm:172.28.2.197:1624781081589:0.149448', 'mem_bu:vm:172.28.2.197:1624781081589:0.006684', 'mem_ca:vm:172.28.2.197:1624781081589:0.084108', 'mem_fr:vm:172.28.2.197:1624781081589:0.850552', 'mem_sr:vm:172.28.2.197:1624781081589:0.00439', 'mem_su:vm:172.28.2.197:1624781081589:0.004312', 'disk_r:vm:172.28.2.197:1624781081589:-1.0', 'disk_w:vm:172.28.2.197:1624781081589:3194421760.0', 'net_o:vm:172.28.2.197:1624781081589:-1.0', 'net_i:vm:172.28.2.197:1624781081589:-1.0', 'intr:vm:172.28.2.197:1624781081589:142957393.0', 'mem_to:vm:172.28.2.197:1624781081589:-1.0', 'cpu_us:vm:172.28.2.198:1624781081589:-1.0', 'cpu_id:vm:172.28.2.198:1624781081589:-1.0', 'cpu_io:vm:172.28.2.198:1624781081589:-1.0', 'cpu_ir:vm:172.28.2.198:1624781081589:0.0', 'cpu_ni:vm:172.28.2.198:1624781081589:-1.0', 'cpu_so:vm:172.28.2.198:1624781081589:0.001556', 'cpu_st:vm:172.28.2.198:1624781081589:0.0', 'cpu_sy:vm:172.28.2.198:1624781081589:0.039333', 'cpu_ur:vm:172.28.2.198:1624781081589:0.063333', 'mem_to:vm:172.28.2.198:1624781081589:-1.0', 'mem_us:vm:172.28.2.198:1624781081589:0.164377', 'mem_bu:vm:172.28.2.198:1624781081589:-1.0', 'mem_ca:vm:172.28.2.198:1624781081589:0.111301', 'mem_fr:vm:172.28.2.198:1624781081589:0.835623', 'mem_sr:vm:172.28.2.198:1624781081589:0.021213', 'mem_su:vm:172.28.2.198:1624781081589:-1.0', 'disk_r:vm:172.28.2.198:1624781081589:946872832.0', 'disk_w:vm:172.28.2.198:1624781081589:-1.0', 'net_o:vm:172.28.2.198:1624781081589:2595.533333', 'net_i:vm:172.28.2.198:1624781081589:-1.0', 'intr:vm:172.28.2.198:1624781081589:73035560.25', 'mem_to:vm:172.28.2.198:1624781081589:-1.0',
                  'cpu_us:vm:172.28.2.199:1624781081589:-1.0', 'cpu_id:vm:172.28.2.199:1624781081589:-1.0', 'cpu_io:vm:172.28.2.199:1624781081589:0.062', 'cpu_ir:vm:172.28.2.199:1624781081589:0.0', 'cpu_ni:vm:172.28.2.199:1624781081589:-1.0', 'cpu_so:vm:172.28.2.199:1624781081589:-1.0', 'cpu_st:vm:172.28.2.199:1624781081589:0.0', 'cpu_sy:vm:172.28.2.199:1624781081589:-1.0', 'cpu_ur:vm:172.28.2.199:1624781081589:-1.0', 'mem_to:vm:172.28.2.199:1624781081589:74274.133333', 'mem_us:vm:172.28.2.199:1624781081589:-1.0', 'mem_bu:vm:172.28.2.199:1624781081589:-1.0', 'mem_ca:vm:172.28.2.199:1624781081589:-1.0', 'mem_fr:vm:172.28.2.199:1624781081589:-1.0', 'mem_sr:vm:172.28.2.199:1624781081589:-1.0', 'mem_su:vm:172.28.2.199:1624781081589:0.006006', 'disk_r:vm:172.28.2.199:1624781081589:-1.0', 'disk_w:vm:172.28.2.199:1624781081589:6533145088.0', 'net_o:vm:172.28.2.199:1624781081589:-1.0', 'net_i:vm:172.28.2.199:1624781081589:3511.533333', 'intr:vm:172.28.2.199:1624781081589:-1.0', 'mem_to:vm:172.28.2.199:1624781081589:74274.133333', 'cpu_us:vm:172.28.2.200:1624781081589:-0.797556', 'cpu_id:vm:172.28.2.200:1624781081589:1.797556', 'cpu_io:vm:172.28.2.200:1624781081589:-1.0', 'cpu_ir:vm:172.28.2.200:1624781081589:0.0', 'cpu_ni:vm:172.28.2.200:1624781081589:-1.0', 'cpu_so:vm:172.28.2.200:1624781081589:-1.0', 'cpu_st:vm:172.28.2.200:1624781081589:-1.0', 'cpu_sy:vm:172.28.2.200:1624781081589:-1.0', 'cpu_ur:vm:172.28.2.200:1624781081589:-1.0', 'mem_to:vm:172.28.2.200:1624781081589:1296975.644444', 'mem_us:vm:172.28.2.200:1624781081589:-1.0', 'mem_bu:vm:172.28.2.200:1624781081589:0.00661', 'mem_ca:vm:172.28.2.200:1624781081589:-1.0', 'mem_fr:vm:172.28.2.200:1624781081589:-1.0', 'mem_sr:vm:172.28.2.200:1624781081589:-1.0', 'mem_su:vm:172.28.2.200:1624781081589:-1.0', 'disk_r:vm:172.28.2.200:1624781081589:1205498368.0', 'disk_w:vm:172.28.2.200:1624781081589:-1.0', 'net_o:vm:172.28.2.200:1624781081589:32540.266667', 'net_i:vm:172.28.2.200:1624781081589:33419.266667', 'intr:vm:172.28.2.200:1624781081589:-1.0', 'mem_to:vm:172.28.2.200:1624781081589:1296975.644444']

def compare(o_e, o):
    oe = o_e
    o = vm_data_to_array(o, [1])  # o_e
    oe = vm_data_to_array(oe, [1])
    if oe == o:
        return True
    else:
        return False

def select_node(filepath, choice):
    file = open(filepath)
    lines = file.readlines()
    file.close()
    if not lines:
        return
    ips = []
    for i in range(len(lines)):
        if choice[i] == 1:
            ips.append(lines[i])
    return ips


def read_data(filepath):
    with open(filepath, "r") as f:
        lines = f.readlines()
        for line in lines:
            line = line.rstrip("\n")
        return lines


vm_attr_path = "./names/names_vm.txt"
# vm_attr_path = "/collector_slave/config/names/names_vm.txt"
vm_ip_path = "/collector_slave/config/ips/ips_slave.conf"
pod_attr_path = "/collector_slave/config/names/names_container.txt"
pod_ip_path = "/collector_slave/config/ips/ips_slave.conf"


def vm_data_to_array(resp, choice):
    ips = ['172.28.2.197', '172.28.2.198', '172.28.2.199', '172.28.2.200']
    temp_dict = {'a': {'b': 1}}
    # temp_dict = {}
    for data in resp:
        es = data.split(":")
        add_2d_dict(temp_dict, es[2], es[0], es[4])
    attrs = read_data(vm_attr_path)
    result = [[0 for i in range(len(attrs))] for i in range(len(ips))]

    i = 0
    for ip in ips:
        ip = ip.rstrip("\n")
        j = 0
        for attr in attrs:
            attr = attr.rstrip("\n")
            if attr in temp_dict[ip]:
                result[i][j] = float(temp_dict[ip][attr])
            else:
                result[i][j] = float('nan')
            j = j + 1
        i = i + 1
    return result


def array_to_vm_data(resp, t):
    ips = ['172.28.2.197', '172.28.2.198', '172.28.2.199', '172.28.2.200']
    attrs = read_data(vm_attr_path)
    result = []
    i = 0
    for unit in resp:
        j = 0
        for v in unit:
            # string = ips[i].rstrip("\n") + " " + attrs[j].rstrip("\n") + " " + str(round(v, 6))
            string = attrs[j].rstrip("\n") + ":vm:" + ips[i].rstrip("\n") + ":" + t + ":" + str(round(float(v), 6))
            result.append(string)
            j = j + 1
        i = i + 1
    return result


def add_2d_dict(d, k_a, k_b, v):
    assert isinstance(d, dict)
    if k_a in d:
        d[k_a].update({k_b: v})
    else:
        d.update({k_a: {k_b: v}})


def float2str(arr):
    res = []
    for i in 4:
        ta=[]
        for j in range(21):
            ta.append(str(round(arr[i][j],6)))
            # temp_arr[i][j]=str(temp_arr[i][j])
        arr.append(ta)
    return arr


def get_ms_time():
    t = time.time()
    return str(round(t * 1000))


def ourpreprocess(temp_arr):
    no=temp_arr.shape[1]
    DSIZE = temp_arr.shape[0]
    for num in range(0,no):
        value=temp_arr[:,num]
        npArray=dataPreprocess(value,DSIZE)
        temp_arr[:, num] = npArray
    return temp_arr

def dataPreprocess(npArray,DSIZE):
    missingValue=pd.notna(npArray)
    print(missingValue)

    # 存在缺失值，进行缺失值填充
    if(pd.isna(npArray).sum()>0):
        # 进行缺失值填充
        npArray[np.isnan(npArray)]=0.0

    # 百分比离群点检测
    npArray=percent_range(npArray,DSIZE, 0.025, 0.975)
    return npArray


# 百分位法:原始参数 min=0.025， max=0.975
def percent_range(dataset,DSIZE, min=0.20, max=0.80):
    range_max = np.percentile(dataset, max * 100)
    range_min = -np.percentile(-dataset, (1 - min) * 100)

    result=np.empty((DSIZE,))
    i=0
    for value in dataset:
        if value <= range_max and value >= range_min:
            result[i]=dataset[i]
        else:
            result[i]=-1

        i+=1
    return result


# 用例目的
print("该用例目的为：")
print("进行预处理系统整体测试，每条数据都能与采集项对应")

# 子用例编号
print("子用例编号：")
print("Ourpreprocess_1")

print("****************************")
print("当前输入为：")
# 输出用例设置
print(input_example)
# 输出用例设置

print("")

print("****************************")
print("当前输出为:")
# 输出处理后数据
t = time.time()
temp = vm_data_to_array(input_example, [1])
temp = ourpreprocess(np.array(temp))
output = array_to_vm_data(temp, get_ms_time())
print(output)
cost = time.time() - t
# 输出处理后数据

print("****************************")
print(output[4])
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
if compare(output, output_example):
    print("输出与预定目标相符")
    print(f"用时：{cost}")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
